async function submitData_spAdd(formID) {
    const form = document.querySelector(formID);
    const tips = document.getElementById('tips');
    /*form.submit(function (event){
        event.preventDefault();  // 阻止表单的默认提交行为
    })*/
    if (form) {
        let v = form.elements.namedItem('summary').value;
        // console.log(v);
        if (v === null || v === "") {
            return;
        }
        v = form.elements.namedItem('paper_count').value;
        // console.log(v,typeof(v));
        if (v === null || parseInt(v) === 0) {
            return;
        }
        v = form.elements.namedItem('amount').value;
        // console.log(v);
        if (v === null || parseFloat(v) === 0.0) {
            return;
        }

        const url = window.location.origin + '/add_sp';

        try {
            let response = await fetch
            (url, {
                method: 'POST',
                body: new FormData(form)
            });
            //response = await response.clone()
            //console.log(response.text())
            // console.log(response)
            if (response.ok) {
                let responseObj = await response.json();
                tips.innerHTML = "提交完成! (提交时间:" + responseObj['edit_time'] + ")";
                //tips.style.backgroundColor = "yellow";
                tableShow_spAdd('sp-parameter', 'table-sticky-paper', responseObj['sp_id'], responseObj['edit_time']);
            }
        } catch (error) {
            tips.innerHTML = "提交失败!";
            tips.style.backgroundColor = "yellow";
            console.log('Request Failed', error);
        }
        //tips.style.transition = "ease-in 0.5s";
        tips.style.backgroundColor = "azure";
    }
}

/* 添加粘贴单后根更新表格 */
function tableShow_spAdd(formID, tableID, idx, editTime) {
    // formID 表单ID,要把其中的数据添加到表格中
    let nodeForm = document.getElementById(formID);
    let nodeTable = document.getElementById(tableID);

    let row = nodeTable.insertRow(-1);
    row.setAttribute('id', 'recorder-sp-' + idx);

    let cell = row.insertCell(0);
    cell.setAttribute('class', 'id')
    cell.innerText = idx;

    cell = row.insertCell(1);
    cell.innerText = nodeForm['summary']

    cell = row.insertCell(2);
    cell.innerText = nodeForm['accounting_title'].value;

    cell = row.insertCell(3);
    cell.innerText = nodeForm['payment_mode'].value;

    cell = row.insertCell(4);
    cell.setAttribute('class', 'number');
    cell.innerText = nodeForm['paper_count'].value;

    cell = row.insertCell(5);
    cell.setAttribute('class', 'number');
    cell.innerText = parseFloat(nodeForm['amount'].value).toFixed(2);
    // 单据状态
    cell = row.insertCell(6);
    cell.setAttribute('class', 'text-right');
    cell.innerText = '010-待审核';

    cell = row.insertCell(7);
    cell.setAttribute('class', 'datetime');
    cell.innerText = editTime;

    cell = row.insertCell(8);
    cell.innerHTML = `<form class="inline-form" method="post" action="/movie/delete/${idx}">
                            <input class="btn" type="submit" name="delete" value="删除" onclick="return confirm('确定要删除本条记录吗?')">
                        </form>

                        <form class="inline-form" method="post">
                            <input class="btn" type="button" name="edit" value="编辑" onclick="trToForm_spEdit('${idx}','recorder-sp-${idx}','sp-parameter_update')">
                        </form> `;
    let spDIV = document.getElementById("mc-sp");
    spDIV.scrollTop = spDIV.scrollHeight;
    clickRowColor(row);
}

async function submitData_spUpdate(formID) {
    const form = document.querySelector(formID);
    const tips = document.getElementById('tips');
    /*form.submit(function (event){
        event.preventDefault();  // 阻止表单的默认提交行为
    })*/
    if (form) {
        const v = form.elements.namedItem('summary_update').value;
        // console.log(v);
        if (v === null || v === "") {
            return;
        }

        const url = window.location.origin + '/update';

        try {
            let response = await fetch(url, {
                method: 'POST',
                body: new FormData(form)
            });
            //response = await response.clone()
            //console.log(response.text())
            // console.log(response)
            if (response.ok) {
                let datetimeStr = await response.text()
                tips.innerHTML = "更新完成! (提交时间:" + datetimeStr + ")";
                //tips.style.backgroundColor = "yellow";
                //更新完成后,更新表格显示
                tableShow_spUpdate('sp-parameter_update', 'recorder-sp-' + form['sp_id'].value, datetimeStr);
                form.elements.namedItem("submit_update").disabled = true;
            }
        } catch (error) {
            tips.innerHTML = "更新失败!";
            tips.style.backgroundColor = "yellow";
            console.log('Request Failed', error);
        }
        //tips.style.transition = "ease-in 0.5s";
        tips.style.backgroundColor = "azure";
    }
}

function tableShow_spUpdate(formID, trID, datetime) {
    let nodeForm = document.getElementById(formID);
    let nodeTr = document.getElementById(trID);

    // 摘要
    nodeTr.cells[1].innerText = nodeForm['summary_update'].value;
    nodeTr.cells[2].innerText = nodeForm['accounting_title_update'].value;
    nodeTr.cells[3].innerText = nodeForm['payment_mode_update'].value;
    nodeTr.cells[4].innerText = nodeForm['paper_count_update'].value;
    nodeTr.cells[5].innerText = parseFloat(nodeForm['amount_update'].value).toFixed(2);
    nodeTr.cells[7].innerText = datetime;
    nodeTr.childNodes.forEach((childx) => {
        if (childx.nodeName === "TD") {
            childx.removeAttribute("style");
        }
    });
    clickRowColor(nodeTr);
}

function trToForm_spEdit(sp_id, trID, formID) {
    let nodeForm = document.getElementById(formID);
    nodeForm.elements.namedItem("submit_update").disabled = false;

    // 把前一个正在编辑黑框去掉
    if (nodeForm['sp_id'].value !== "") {
        let nodeTr = document.getElementById('recorder-sp-' + nodeForm['sp_id'].value);
        nodeForm.elements.namedItem("submit_update").disabled = false;
        nodeTr.childNodes.forEach((childx) => {
            if (childx.nodeName === "TD") {
                childx.removeAttribute("style");
            }
        });
    }

    let nodeTr = document.getElementById(trID);
    let isFirst = true;
    let lastTD = null;
    // console.log(nodeTr);
    nodeTr.childNodes.forEach((childx) => {
        if (childx.nodeName === "TD") {
            if (isFirst) {
                childx.style.borderLeftWidth = "2px";
                childx.style.borderLeftColor = "black";
                isFirst = false;
            }
            childx.style.borderTopWidth = "2px";
            childx.style.borderTopColor = "black";
            childx.style.borderBottomWidth = "2px";
            childx.style.borderBottomColor = "black";
            lastTD = childx;
            // console.log(key);
        }
    });
    if (lastTD != null) {
        lastTD.style.borderRightWidth = "2px";
        lastTD.style.borderRightColor = "black";
    }
    let summary_update = nodeForm.elements.namedItem('summary_update');
    let cellTemp = nodeTr.cells[1];
    summary_update.value = cellTemp.innerText;

    let accounting_title_update = nodeForm.elements.namedItem('accounting_title_update');
    cellTemp = nodeTr.cells[2];
    accounting_title_update.value = cellTemp.innerText;
    //accounting_update.setAttribute("value", cellTemp.innerText);

    let payment_mode_update = nodeForm.elements.namedItem('payment_mode_update');
    cellTemp = nodeTr.cells[3];
    payment_mode_update.value = cellTemp.innerText;

    let paper_count_update = nodeForm.elements.namedItem('paper_count_update');
    cellTemp = nodeTr.cells[4];
    paper_count_update.value = cellTemp.innerText;

    let amount_update = nodeForm.elements.namedItem('amount_update');
    cellTemp = nodeTr.cells[5];
    amount_update.value = cellTemp.innerText;

    let spID = nodeForm.elements.namedItem('sp_id');
    spID.value = sp_id;
    // console.log('v1:' + v1.value, 'v2:' + v2.innerText);
}

function clickRowColor(element) {
    let elementTable = document.getElementById("table-sticky-paper");
    console.log(elementTable.rows.length);
    for (let row of elementTable.rows) {
        if (row.rowIndex !== 0) {
            row.style.backgroundColor = "white"
        }
    }
    element.style.backgroundColor = 'wheat';
}

function mouseenterColor(element) {
    if (element.style.backgroundColor === 'white') {
        element.style.backgroundColor = 'azure';
    }
}

function mouseleaveColor(element) {
    if (element.style.backgroundColor === 'azure') {
        element.style.backgroundColor = 'white';
    }
}

function closeModalFrame(element) {
    //event.stopPropagation()
    //if (element.style.visibility === 'visible') {
        element.style.visibility = 'hidden';
    //}
    console.log("显示模式为：" + element.style.visibility);
    console.log('代码运行');
    console.log(element.id)
}